package com.wjh.controller;
import com.wjh.enums.AppHttpCodeEnum;
import com.wjh.exception.SystemException;
import com.wjh.pojo.Menu;
import com.wjh.pojo.User;
import com.wjh.service.LoginService;
import com.wjh.service.MenuService;
import com.wjh.utils.ResponseResult;
import com.wjh.utils.SecurityUtils;
import com.wjh.vo.RoutersVo;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping
public class LoginController {

    @Resource
    private LoginService loginService;

    @Resource
    private MenuService menuService;

    @PostMapping("/user/login")
    public ResponseResult login(@RequestBody User user){
        if(!StringUtils.hasText(user.getUserName())){
            //提示必须要传用户名
            throw new SystemException(AppHttpCodeEnum.REQUIRE_USERNAME);
        }
        return loginService.login(user);
    }

    @PostMapping("/logout")
    public ResponseResult logout(){
        return loginService.logout();
    }

    @GetMapping("getInfo")
    public ResponseResult getInfo(){
        return loginService.getInfo();
    }

    @GetMapping("getRouters")
    public ResponseResult<RoutersVo> getRouters(){
        //获取当前用户Id
        Integer userId = SecurityUtils.getUserId();
        //查询menu 结果是tree的形式
        List<Menu> menus = menuService.selectRouterMenuTreeByUserId(userId);
        return ResponseResult.okResult(new RoutersVo(menus));
    }
}
